package com.itheima.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.itheima.pojo.db.PmsBrand;
import com.itheima.service.PmsBrandService;
import com.itheima.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 芝士雪豹
 * @create 2022/10/11/11:12
 */
@RestController
@RequestMapping("/brand")
public class PmsBrandController {
    @Autowired
    private PmsBrandService pmsBrandService;
    /**
     * 添加品牌
     *
     * @param pmsBrand
     * @return
     */
    @PostMapping("create")
    public ResponseEntity createBrand(@RequestBody PmsBrand pmsBrand) {
        pmsBrandService.createBrand(pmsBrand);
        return ResponseEntity.ok("添加成功");
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete/batch")
    public ResponseEntity deletes(List<Integer> ids) {
        if (ObjectUtils.isEmpty(ids)) {
            throw new RuntimeException("还未选择要删除的品牌,请选择后再试");
        }
        pmsBrandService.delete(ids);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public ResponseEntity deleteById(@PathVariable("id") Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new RuntimeException("还未选择要删除的品牌,请选择后再试");
        }
        pmsBrandService.deleteById(id);
        return ResponseEntity.ok("删除");
    }

    /**
     * 根据条件分页查询品牌
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("list")
    public ResponseEntity pageByCondition(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                  String keyword) {
        PageResult<PmsBrand> page = pmsBrandService.pageByCondition(pageNum, pageSize, keyword);
        return ResponseEntity.ok(page);
    }

    /**
     * 查询所有品牌
     *
     * @return
     */
    @GetMapping("listAll")
    public ResponseEntity listAll() {
        List<PmsBrand> pmsBrands = pmsBrandService.listAll();
        return ResponseEntity.ok(pmsBrands);
    }

    /**
     * 批量修改厂家制造商状态
     *
     * @param factoryStatus
     * @param ids
     * @return
     */
    @PostMapping("/update/factoryStatus")
    public ResponseEntity upateFactoryStatus(@RequestParam("factoryStatus") Integer factoryStatus,
                                     @RequestParam("ids") List<Integer> ids) {
        pmsBrandService.upateFactoryStatus(factoryStatus, ids);

        return ResponseEntity.ok("操作成功");

    }

    /**
     * 批量修改显示状态
     *
     * @param showStatus
     * @param ids
     * @return
     */
    @PostMapping("/update/showStatus")
    public ResponseEntity updateShowStatus(@RequestParam("showStatus") Integer showStatus,
                                   @RequestParam("ids") List<Integer> ids) {
        pmsBrandService.upateShowStatus(showStatus, ids);

        return ResponseEntity.ok("操作成功");
    }

    /**
     * 修改单个品牌
     *
     * @param id
     * @param pmsBrand
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateById(@PathVariable("id") Integer id,
                             @RequestBody PmsBrand pmsBrand) {
        pmsBrand.setId(id.longValue());
        pmsBrandService.updateById(pmsBrand);
        return ResponseEntity.ok("操作成功");
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<PmsBrand> findById(@PathVariable Integer id) {
        PmsBrand pmsBrand = pmsBrandService.findById(id.longValue());
        return ResponseEntity.ok(pmsBrand);
    }

}
